package com.example.demo.service.exam.impl;

import com.example.demo.domain.CombineClause;
import com.example.demo.domain.SingleClause;
import com.example.demo.domain.exam.ExamUserSingleQuesScore;
import com.example.demo.exception.ExceptionUtils;
import com.example.demo.mapper.ExamUserSingleQuesScoreMapper;
import com.example.demo.service.exam.ExamUserSingleQuesScoreService;
import com.example.demo.service.impl.BaseServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.sql.Timestamp;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class ExamUserSingleQuesScoreServiceImpl extends BaseServiceImpl<ExamUserSingleQuesScoreMapper, ExamUserSingleQuesScore> implements ExamUserSingleQuesScoreService {

    private final ExamUserSingleQuesScoreMapper mapper;

    @Override
    public void insertList(List<ExamUserSingleQuesScore> list, Integer totalScore) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        Timestamp currentTime = new Timestamp(System.currentTimeMillis());
        Double sum = list.stream().map(e -> {
            valid(e);
            e.setCreateTime(currentTime);
            return e.getScore();
        }).reduce(0d, Double::sum);
        if (sum > totalScore) {
            throw ExceptionUtils.exception("总分不能大于考试总分：%s分", totalScore);
        }
        super.saveOrUpdateBatch(list);
    }

    @Override
    public void insertOrUpdate(ExamUserSingleQuesScore entity) {
        // valid(entity);
        super.saveOrUpdate(entity);
    }

    @Override
    public List<ExamUserSingleQuesScore> getByUserExamId(Long userId, Long examId) {
        if (userId == null) {
            throw ExceptionUtils.exception("用户标识不能为空");
        }
        if (examId == null) {
            throw ExceptionUtils.exception("考试标识不能为空");
        }
        return super.selectList(CombineClause.and(
                SingleClause.equal("user_id", userId),
                SingleClause.equal("exam_id", examId)
        ));
    }

    @Override
    public List<ExamUserSingleQuesScore> getByUserExamQuestionId(Long userId, Long examId, Long questionId) {
        if (userId == null) {
            throw ExceptionUtils.exception("用户标识不能为空");
        }
        if (examId == null) {
            throw ExceptionUtils.exception("考试标识不能为空");
        }
        if (questionId == null) {
            throw ExceptionUtils.exception("题目标识不能为空");
        }
        return super.selectList(CombineClause.and(
                SingleClause.equal("user_id", userId),
                SingleClause.equal("exam_id", examId),
                SingleClause.equal("question_id", questionId)

        ));
    }

    @Override
    public void insertOrUpdateList(List<ExamUserSingleQuesScore> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        super.saveOrUpdateBatch(list);
    }

    private void valid(ExamUserSingleQuesScore entity) {
        Optional.ofNullable(entity.getExamId()).orElseThrow(() -> ExceptionUtils.exception("考试标识不能为空"));
        Optional.ofNullable(entity.getClassId()).orElseThrow(() -> ExceptionUtils.exception("班级标识不能为空"));
        Optional.ofNullable(entity.getPaperId()).orElseThrow(() -> ExceptionUtils.exception("试卷标识不能为空"));
        Optional.ofNullable(entity.getQuestionId()).orElseThrow(() -> ExceptionUtils.exception("问题标识不能为空"));
        Optional.ofNullable(entity.getUserId()).orElseThrow(() -> ExceptionUtils.exception("用户标识不能为空"));
    }

    @Override
    public void deleteByExamUserId(Long examId, Long userId) {
        Optional.ofNullable(examId).orElseThrow(() -> ExceptionUtils.exception("考试标识不能为空"));
        Optional.ofNullable(userId).orElseThrow(() -> ExceptionUtils.exception("用户标识不能为空"));
        super.delete(CombineClause.and(
                SingleClause.equal("exam_id", examId),
                SingleClause.equal("user_id", userId)
        ));
    }

    @Override
    public List<ExamUserSingleQuesScore> getByExamId(Long examId) {
        Optional.ofNullable(examId).orElseThrow(() -> ExceptionUtils.exception("考试标识不能为空"));
        return super.selectList(SingleClause.equal("exam_id", examId));
    }

    @Override
    public void insertBatch(List<ExamUserSingleQuesScore> scores) {
        if (CollectionUtils.isEmpty(scores)) {
            return;
        }
        super.saveBatch(scores);
    }
}
